﻿using System;
using System.Management;

namespace DesignPattern.hardWare {
    public class HardWare {

        public void Test(){
            
            ManagementObjectSearcher objvide = new ManagementObjectSearcher("select * from Win32_VideoController");

            foreach (ManagementObject obj in objvide.Get())
            {
                System.Console.WriteLine("Name - " + obj["Name"] + "</br>");
                System.Console.WriteLine("DeviceID - " + obj["DeviceID"] + "</br>");
                System.Console.WriteLine("AdapterRAM - " + obj["AdapterRAM"] + "</br>");
                System.Console.WriteLine("AdapterDACType - " + obj["AdapterDACType"] + "</br>");
                System.Console.WriteLine("Monochrome - " + obj["Monochrome"] + "</br>");
                System.Console.WriteLine("InstalledDisplayDrivers - " + obj["InstalledDisplayDrivers"] + "</br>");
                System.Console.WriteLine("DriverVersion - " + obj["DriverVersion"] + "</br>");
                System.Console.WriteLine("VideoProcessor - " + obj["VideoProcessor"] + "</br>");
                System.Console.WriteLine("VideoArchitecture - " + obj["VideoArchitecture"] + "</br>");
                System.Console.WriteLine("VideoMemoryType - " + obj["VideoMemoryType"] + "</br>");
            }
            
//            Console.WriteLine(GetUserName());
//            Console.WriteLine(GetCpuID());
//            Console.WriteLine(GetTotalPhysicalMemory());
        }
        
        /// <summary>  
        /// 操作系统的登录用户名  
        /// </summary>  
        /// <returns></returns>  
        private string GetUserName()  
        {  
            try  
            {  
                string st = "";  
                ManagementClass mc = new ManagementClass("Win32_ComputerSystem");  
                ManagementObjectCollection moc = mc.GetInstances();  
                foreach (ManagementObject mo in moc)  
                {   
                    st = mo["UserName"].ToString();   
                }  
                moc = null;  
                mc = null;  
                return st;  
            }  
            catch  
            {  
                return "unknow";  
            }   
        }
        
        /// <summary>  
        /// 物理内存  
        /// </summary>  
        /// <returns></returns>  
        string GetTotalPhysicalMemory()  
        {  
            try  
            {  
  
                string st = "";  
                ManagementClass mc = new ManagementClass("Win32_ComputerSystem");  
                ManagementObjectCollection moc = mc.GetInstances();  
                foreach (ManagementObject mo in moc)  
                {   
                    st = mo["TotalPhysicalMemory"].ToString();   
                }  
                moc = null;  
                mc = null;  
                return st;  
            }  
            catch  
            {  
                return "unknow";  
            }   
        }  
        
        private string GetCpuID(){
            try{
                //获取CPU序列号代码  
                string cpuInfo = ""; //cpu序列号  
                ManagementClass mc = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc){
                    cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                }
                
                moc = null;
                mc = null;
                return cpuInfo;
            }
            catch{
                return "unknow";
            }
        }
    }
}